// pages/wxPay/wxPay.js
var server = require('../../utils/server');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    payParam: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

     alert(11);return false; 
   var app = getApp();
    app.getOpenId(function () {

      var openId = getApp().globalData.openid;
      var order_id =options.order_id;
 
       server.getJSON('/Payment/get_wx_mini_pay/openid/' + openId + "/order_id/" + order_id, function (res){
        if(res.statusCode==200){//获取参数成功
          var timeStamp=res.data.timeStamp;
          var prepay_id=res.data.package;
          var paySign=res.data.paySign;
          var signType=res.data.signType;
          var nonceStr=res.data.nonceStr;
          wx.requestPayment({
              "timeStamp": timeStamp,
              "package":prepay_id,
              "paySign":paySign,
              "signType":signType,
              "nonceStr":nonceStr,
              // "paySign": _this.data.payParam.paySign,
              //支付成功的回调
              'success': function (res) {

                    wx.showToast({
                      title: '支付成功',
                      icon: 'none',
                  });
                   wx.navigateTo({
                       url:"/pages/index/index",
                   });
              },
              //支付失败的回调
              'fail': function (res) {
                  // console.log('fail:' + JSON.stringify(res));
                  wx.showToast({
                      title: '支付失败:' + JSON.stringify(res),
                      icon: 'none',
                  })
                   wx.navigateTo({
                       url:"/pages/index/index",
                   });
              },
              'complete': function (res) {
                 wx.showToast({
                      title: 'complete:' + JSON.stringify(res),
                      icon: 'none',
                  })
                   wx.navigateTo({
                       url:"/pages/index/index",
                   });
                  // console.log('complete:' + JSON.stringify(res));
              }
          });
        }
      }); 
    }); 
  },
  //采用小程序支付
  confirmPay: function () {
    // console.log('wxpay');
    const _this = this;

      // console.log(_this.data.payParam.appId);
    wx.requestPayment(
      {
        "timeStamp": _this.data.payParam.timestamp,
        "package": 'prepay_id='+_this.data.payParam.prepay_id,
        "paySign": _this.data.payParam.paySign,
        "signType": _this.data.payParam.signType,
        "nonceStr": _this.data.payParam.nonceStr,
        // "paySign": _this.data.payParam.paySign,
         //支付成功的回调
        'success': function (res) {      
          
            

          console.log('success:' + JSON.stringify(res));
        },
        //支付失败的回调
        'fail': function (res) {
          console.log('fail:' + JSON.stringify(res));
          wx.showToast({
            title: '支付失败:' + JSON.stringify(res),
            icon: 'none',
          })

        },
        'complete': function (res) {
          console.log('complete:' + JSON.stringify(res));
        }
      })
  }

})